<template>
  <page-form-row :gutter="0">
    <priceFormCell
      :unit="unit"
      label="启程"
      :field="`${fieldPrefix ? fieldPrefix + '.' : ''}${priceType}`"
      v-model="mergedRecord[priceType]"
      :record="mergedRecord"
      :marginBottom="hasChannelPrice ? 6 : 0"
    >
      <template #label-extra v-if="record.channelPriceList?.length > 0">
        <a-button
          :disabled="!mergedRecord[priceType] && mergedRecord[priceType] != 0"
          class="one-write"
          type="text"
          size="mini"
          @click="oneKeyAllWrite(mergedRecord)"
          v-if="hasChannelPrice"
        >
          一键全写
        </a-button>
      </template>
      <!-- 周末价需要配置 -->
      <price-weekday-check v-if="priceType == 'weekendPrice'" v-model="mergedRecord.weekendDayList" />
    </priceFormCell>
    <!-- 非基础保险、租车押金、违章押金情况下 -->
    <template v-if="hasChannelPrice">
      <priceFormCell
        :unit="unit"
        class="custom-form-item"
        :label="item.sourceIdName"
        v-for="(item, index) in mergedRecord.channelPriceList"
        :key="index"
        :record="item"
        v-model="item[priceType]"
        :priceType="priceType"
        :field="`${fieldPrefix ? fieldPrefix + '.' : ''}channelPriceList.${index}.${priceType}`"
        :marginBottom="index != mergedRecord.channelPriceList.length - 1 ? 6 : 0"
      >
        <!-- 周末价 -->
        <template v-if="priceType == 'weekendPrice'">
          <price-weekday-check v-if="item.sourceIdName != '飞猪'" v-model="item.weekendDayList" />
          <!-- 飞猪展示不支持设置 toolTip -->
          <a-tooltip v-else content="飞猪不支持针对车型设置周末时间，仅支持针对门店设定，请前往飞猪-门店中配置" position="top">
            <img src="@/assets/price-tip.png" style="width: 16px; height: 16px; margin-left: 8px" />
          </a-tooltip>
        </template>
      </priceFormCell>
    </template>
  </page-form-row>
</template>
<script setup name="sorderConfigPriceForm">
  import priceFormCell from './price-form-cell.vue'
  import priceWeekdayCheck from './price-weekday-check.vue'
  import { inject, computed } from 'vue'
  import { useVModel } from '@vueuse/core'

  const formRef = inject('formRef')

  const props = defineProps({
    record: Object,
    fieldPrefix: String, // fieldId 前缀
    priceType: String, // 费用类型
  })

  const unit = computed(() => {
    if (['poundageAmount', 'carDeposit', 'illegalDeposit'].includes(props.priceType)) {
      return '元/次'
    }
    return '元/日'
  })

  const emits = defineEmits(['update:record'])
  const mergedRecord = useVModel(props, 'record', emits)

  // 显示一键全写按钮
  const hasChannelPrice = computed(() => {
    return !['insuranceAmount', 'carDeposit', 'illegalDeposit'].includes(props.priceType) && props.record.channelPriceList?.length > 0
  })

  // 一键全写
  const oneKeyAllWrite = (record) => {
    const type = props.priceType
    // 更新渠道数据
    record.channelPriceList.forEach((item) => {
      // 手续费
      if (type == 'poundageAmount') {
        // 哈啰无法反写手续费
        if (item.sourceId != '3') {
          item[type] = record[type]
        }
      }
      // 周末价需要额外更新周末配置
      else if (type == 'weekendPrice') {
        item[type] = record[type]
        item.weekendDayList = record.weekendDayList
      } else {
        item[type] = record[type]
      }
    })
    // 校验数据是否合规
    let fields = record.channelPriceList.map((item, index) => {
      return `${props.fieldPrefix ? props.fieldPrefix + '.' : ''}channelPriceList.${index}.${type}`
    })
    formRef.value.validateField(fields)
  }
</script>
<style lang="less" scoped></style>
